package com.deercrowing.takeaway.controller.user;


import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.vo.ShoppingCartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

/**
 * 购物车(ShoppingCart)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-22 15:21:22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("user/shoppingcart")
public class ShoppingCartController {
    /**
     * 服务对象
     */
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll() {
        return ResponseEntity.ok(Result.OK(this.shoppingCartService.selectListByShoppingCart()));
    }

    /**
     * 新增数据
     *
     * @param shoppingCartVO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody ShoppingCartVO shoppingCartVO) {
        if (ObjectUtils.isEmpty(shoppingCartVO)){
            throw new NullPointerException();
        }

        this.shoppingCartService.insertByShoppingCart(shoppingCartVO);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, shoppingCartEntity 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Long id, @RequestBody ShoppingCartVO shoppingCartVO) {
        if (ObjectUtils.isEmpty(this.shoppingCartService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok("this.shoppingCartService.updateById(shoppingCartEntity)");
    }

    /**
     * 删除数据
     *
     * @param shoppingCartVO 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/clean")
    public ResponseEntity deleteAll(@RequestBody ShoppingCartVO shoppingCartVO) {
        if (ObjectUtils.isEmpty(shoppingCartVO)){
            throw new NullPointerException();
        }
        this.shoppingCartService.cleanShoppingCart();

        return ResponseEntity.ok(Result.OK());
    }

    @DeleteMapping
    public ResponseEntity delete(@RequestBody ShoppingCartVO shoppingCartVO){
        if (ObjectUtils.isEmpty(shoppingCartVO)){
            throw new NullPointerException();
        }
        this.shoppingCartService.deleteByShoppingCart(shoppingCartVO);

        return ResponseEntity.ok(Result.OK());
    }
}

